package cn.mynb.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * 打开Session
 * 作者：孤独的狼
 * 日期：2009-10-4-下午07:38:03
 * 项目名称：shoa
 * 全路径名：cn.serup.shoa.util.HibernateUtil.java
 */
public class HibernateUtil {

	private static SessionFactory sessionFactory;

	static {
		try {
			Configuration cfg = new Configuration().configure();

			sessionFactory = cfg.buildSessionFactory();
		} catch (HibernateException e) {
			System.out.println("找不到Hibernate.cfg.xml配置文件，或配置有错");
			e.printStackTrace() ;
		} 
	}

	public static Session getSession() {
		return sessionFactory.openSession();
	}

	public static void closeSession(Session session) {
		if (session != null) {
			if (session.isOpen()) {
				session.flush() ;
				session.close();
			}
		}
	}

	public static void main(String[] args) {
		Session session = HibernateUtil.getSession() ;
		System.out.println(session);
	}

}
